package com.wrs.examsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Answer;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Student;
import com.wrs.examsystem.entity.User;
import com.wrs.examsystem.mapper.AnswerMapper;
import com.wrs.examsystem.mapper.StudentMapper;
import com.wrs.examsystem.mapper.UserMapper;
import com.wrs.examsystem.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private AnswerMapper answerMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Integer findTotalMark(String studentNumber,Integer testId) {
//        根据学号查询学生ID
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_number",studentNumber);
        Student student = studentMapper.selectOne(studentQueryWrapper);
//        根据ID查询学生的答案以及评价过的分数
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        answerQueryWrapper.eq("student_id",student.getStudentId());
        answerQueryWrapper.eq("test_id",testId);
        List<Answer> studentAnswer = answerMapper.selectList(answerQueryWrapper);
//        System.out.println("===================");
//        System.out.println(studentAnswer.get(0).toString());
//        检验是否已经批阅过
//        for (int i = 0; i < studentAnswer.size(); i++) {
//
//        } 暂时无法检验
//        计算得分
        Integer totalMark = 0;
        for (int i = 0; i < studentAnswer.size(); i++) {
            totalMark += studentAnswer.get(i).getAnswerMark();
        }
        System.out.println("===========================");
        System.out.println(totalMark);
        return totalMark;
    }

    /**
     * 逻辑删除，同时删除学生表中的和用户表中的还有成绩表中的
     * @param studentNumber
     * @return
     */

    @Override
    public R deleteByStudentNumber(String studentNumber) {
//        删除学生表中的
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_number",studentNumber);
        Student student = studentMapper.selectOne(studentQueryWrapper);
        int delete = studentMapper.delete(studentQueryWrapper);
        if (delete == 0){
            return R.error().message("删除失败");
        }
//        删除用户表中的
        int deleteById = userMapper.deleteById(student.getUserId());
        if (deleteById == 0){
            return R.error().message("删除失败");
        }
//        删除答案表中的
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        answerQueryWrapper.eq("student_id",student.getStudentId());
        answerMapper.delete(answerQueryWrapper);
        return R.ok().message("删除成功");
    }

    @Override
    public Boolean findStudentByStudentNumber(String studentNumber) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_number",studentNumber);
        Student student = studentMapper.selectOne(studentQueryWrapper);
        return student != null;
    }

    @Override
    public R checkUser(Student checkStudent) {
        Student student = studentMapper.selectById(checkStudent.getStudentId());
        if (student.getStudentNumber().equals(checkStudent.getStudentNumber())
                && student.getStudentName().equals(checkStudent.getStudentName())
                && student.getStudentSex().equals(checkStudent.getStudentSex())
                && student.getStudentClass().equals(checkStudent.getStudentClass())){
            return R.ok();
        }
        return R.error();
    }
}
